$("#all").css("height",$(window).height()); //浏览器当前窗口可视区域高度
var form = new mini.Form("form1");
	mini.parse();
   var projectName = $("#projectName").val();
    var tree = mini.get("treegrid1");
    var roleList = mini.get("roleList");
    roleList.on("valuechanged", function (e) {
      changedRole(this.getValue());
    });
    var showAllSelect = true;
   function ondrawcell(e) {
         var tree = e.sender,
            record = e.record,            
            column = e.column,
            field = e.field,
            id = record[tree.getIdField()],
            funs = record.resourceList;

         function createCheckboxs(funs) {
            if (!funs) return "";
            var html = "";
            if (showAllSelect) {
                var value = record.checkAll !== false ? "全选" : "取消";
                
                var clickFn = 'checkAllFunc(\'' + id + '\', this)';
                html += '<input onclick="' + clickFn + '" type="button" value="' + value + '" style="border:solid 1px #aaa;"/>';
            }
            for (var i = 0, l = funs.length; i < l; i++) {
                var fn = funs[i];
                var clickFn = 'checkFunc(\'' + id + '\',\'' + fn.resourceId+ '\', this.checked)';
                var checked = fn.checked ? 'checked' : '';																		
                html += '<label class="function-item"><input onclick="' + clickFn + '" ' + checked + ' type="checkbox" value="'+fn.resourceId+'" id="'+fn.resourceId+'" name="checkBoxFunction" hideFocus/>' + fn.resourceName + '</label>';
            }
            return html;
        } 

        if (field == 'resourceList') {
            e.cellHtml = createCheckboxs(funs);
        } 
    }

    function saveFuns() {
    	
    	form.validate();
        if (form.isValid() == false){
        	mini.alert("请选择角色！");
        	return false;
        }
        mini.mask({
            el: document.body,
            cls: 'mini-mask-loading',
            html: '正在保存...'
        }); 
        var roleid=mini.get("roleList").getValue();
    	var functionCodes="";
    	var roleEntity= '{"roleId":"'+roleid+'","resourceList":[@123]}';
    	 var size = ($("[name='checkBoxFunction'][checked]").length)-1;
    	 $("[name='checkBoxFunction'][checked]").each(function(i){
    		 		if(this.checked){
    		 			if(i==size){
    		 				roleEntity =roleEntity.replace('@123', '{"resourceId":"'+$(this).val()+'"}');
    		 			}else{
    		 				roleEntity =roleEntity.replace('@123', '{"resourceId":"'+$(this).val()+'"},@123');
    		 			}
    		 		
    			    }
    	 });
    	 var jsons = mini.decode (roleEntity,false);
    	 var json = mini.encode (jsons)
    	 $.ajax({
    			url : projectName+"/sys/SaveRoleWithFunction",
    			type : 'post',
    			contentType : "application/json; charset=utf-8",
    			dataType : 'json',
    			data : json,
    			cache : false,
    			success : function(data) {
    				mini.unmask();
    				mini.alert("保存成功");
    			},
    			error : function(jqXHR, textStatus, errorThrown) {
    				mini.alert(jqXHR.responseText);
    				mini.unmask();
    			}
    		});
    } 	 
    function checkFunc(id, action, checked) {
    	 if($("#"+action).attr("checked")){
    		 $("#"+action).attr("checked",true); 
    	 }else{
    		 $("#"+action).attr("checked",false); 
    	 };
    }
    function checkAllFunc(id, btn) {
        var record = tree.getRecord(id);
        if (!record) return;
        var funs = record.resourceList;
        if (!funs) return;


        var checked = record.checkAll !== false;
        
        for (var i = 0, l = funs.length; i < l; i++) {
            var o = funs[i];
            o.checked = checked;
        }

        record.checkAll = !checked;
        tree.updateRow(record);
        
    }
    function changedRole(roleid){
    		var roleEntity= {roleId:roleid};
    	   $(":checkbox").attr("checked", false);
    	mini.mask({
            el: document.body,
            cls: 'mini-mask-loading',
            html: '正在查询...'
        }); 
    	$.ajax({
    		url : projectName+"/sys/queryMenuAndButtonWithRoleId",
    		type : 'post',
    		dataType : 'json',
    		data : roleEntity,
    		cache : false,
    		success : function(data) {
    			mini.unmask();
    			checkFunctions(data);
    		},
    		error : function(jqXHR, textStatus, errorThrown) {
    			mini.alert(jqXHR.responseText);
    			mini.unmask();
    		}
    	});
    }
   function checkFunctions(data){
	   if(data.length<1){return;}
	   for(var i=0;i<data.length;i++){
		 $("#"+data[i].resourceId).attr("checked", true);
	   }
   }
    
   
   function nodeCheck(tree,node,flag){
	   mini.alert(flag)
   }